<template>
    <div>
        <div class="crumbs">
            <el-breadcrumb separator="/">
                <el-breadcrumb-item>
                    <i class="el-icon-lx-cascades"></i> 用户绑定
                </el-breadcrumb-item>
            </el-breadcrumb>
        </div>
        <div class="container">
            <div class="handle-box">
                <el-input v-model="query.mobile"  placeholder="手机号"  class="handle-input mr10"></el-input>

                <el-select v-model="query.state" :clearable="true" class="mr10" @clear="query.state=null" placeholder="绑定状态">
                    <el-option
                            v-for="item in stateOptions"
                            :key="item.key"
                            :label="item.value"
                            :value="item.key">
                    </el-option>
                </el-select>

                <el-button type="primary" icon="el-icon-search" @click="handleSearch">搜索</el-button>
            </div>
            <el-table
                :data="tableData"
                border
                class="table"
                ref="multipleTable"
                header-cell-class-name="table-header"
                :cell-style="cellStyle"
            >
                <el-table-column prop="id" label="Id" width="55" align="center"></el-table-column>
                <el-table-column prop="miyou_id" label="UID"></el-table-column>
                <el-table-column prop="mobile" label="手机号"></el-table-column>
                <el-table-column prop="nickname" label="昵称"></el-table-column>
                <el-table-column prop="hide_mobile" label="直推业绩"></el-table-column>
                <el-table-column prop="register_time" label="注册时间"></el-table-column>
                <el-table-column prop="state_txt" style="color: red" label="绑定状态"></el-table-column>
                <el-table-column label="操作" width="180" align="center">
                    <template slot-scope="scope">
                        <el-button
                                type="text"
                                @click="beginEdit(scope.row)"
                        >修改绑定手机</el-button>
                    </template>
                </el-table-column>
            </el-table>
            <div class="pagination">
                <el-pagination
                    background
                    layout="prev, next, pager"
                    :current-page="query.page"
                    :page-size="query.pageSize"
                    :total="pageTotal"
                    @current-change="handlePageChange"
                ></el-pagination>
            </div>
            <el-dialog
                    title="修改手机号绑定"
                    :visible.sync="editVisible"
                    width="30%">
                <el-form ref="form" :model="form" label-width="100px">
                    <el-form-item label="手机号码">
                        <el-input v-model="form.mobile"></el-input>
                    </el-form-item>
                </el-form>
                <span slot="footer" class="dialog-footer">
                <el-button @click="editVisible = false">取 消</el-button>
                <el-button type="primary" @click="doEdit()">确 定</el-button>
                </span>
            </el-dialog>

            <el-dialog
                    title="同步管理后台用户信息"
                    :visible.sync="syncVisible"
            >

                <el-form ref="form"  label-width="150px">
                    <el-form-item label="管理后台SESSION">
                        <el-input v-model="session_key"></el-input>
                    </el-form-item>

                </el-form>
                <span slot="footer" class="dialog-footer">
                <el-button @click="syncVisible = false">取 消</el-button>
                <el-button type="primary" @click="doSet()">确 定</el-button>
                </span>
            </el-dialog>
        </div>
    </div>
</template>

<script>

export default {
    name: 'order',
    data() {
        return {
            query: {
                page: 1,
                state: null,
                mobile: '',
                pageSize: 10,
            },
            stateOptions: [
                {key: 0, value: "未绑定"},
                {key: 1, value: "已绑定"},
                {key: 2, value: "绑定失败"},
            ],
            tableData: [],
            pageTotal: 0,
            editVisible: false,
            syncVisible: false,
            session_key: '',
            form: {
                id : 0,
                mobile : '',
            }
        };
    },
    created() {
        this.getData();
    },
    methods: {
        getData() {
            this.http.miyouUser(this.query).then(res => {
                if(res.code ===  0) {
                    this.tableData = res.data.data;
                    this.pageTotal = res.data.total;
                } else {
                    this.$message.error(res.msg);
                }
            });
        },
        cellStyle(row,column,rowIndex,columnIndex){ //根据报警级别显示颜色
            if(row.column.label==="绑定状态" && row.row.state===1){
                return 'color:green'
            }else if(row.column.label==="绑定状态"&& row.row.state===2 ){
                return 'color:red'
            }
        },
        // 触发搜索按钮
        handleSearch() {
            this.$set(this.query, 'page', 1);
            this.getData();
        },
        // 编辑操作
        confirmOk(index, row) {
            this.http.doWithdraw({id: row.id}).then(res => {
                this.getData();
            });
        },
        // 分页导航
        handlePageChange(val) {
            this.$set(this.query, 'page', val);
            this.getData();
        },
        beginEdit(user) {
            this.form.mobile = user.mobile ? user.mobile: user.hide_mobile
            this.form.id = user.id
            this.editVisible = true
        },
        doEdit() {
            this.http.userBind(this.form).then(res => {
                this.getData();
            });
            this.editVisible = false
        },
        doSet() {
            this.http.setSession({session_key: this.session_key}).then(res => {
                this.$message.success("同步成功")
            });
            this.syncVisible = false
        },
    }
};
</script>

<style scoped>
.handle-box {
    margin-bottom: 20px;
}

.handle-select {
    width: 120px;
}

.handle-input {
    width: 300px;
    display: inline-block;
}
.table {
    width: 100%;
    font-size: 14px;
}
.red {
    color: #ff0000;
}
.mr10 {
    margin-right: 10px;
}
.table-td-thumb {
    display: block;
    margin: auto;
    width: 40px;
    height: 40px;
}
</style>
